﻿using RentalIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RentalManagement.Controllers
{
    public class StaffController : Controller
    {

        //IOC构造函数调用[第二种调用方式]
        private readonly IServiceStaff _staffService;
        private readonly IServiceRole _Role;
        public StaffController(IServiceStaff staffService, IServiceRole Role)
        {
            _staffService = staffService;
            _Role = Role;
        }


        // GET: Staff
        public ActionResult Index()
        {
            ViewBag.role = _Role.SelectRoleID("Y");
            return View();
        }

        public ActionResult ShowStaff(int offset, int limit, string sort, string order, string name)
        {
            int count = 0;//总数据量
            if (String.IsNullOrWhiteSpace(sort))
            {
                sort = "Id";
            }
            var lst = _staffService.ShowStaff(offset, limit, sort, order, name,ref count);
            return Json(new { total = count, rows = lst }, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 添加,修改
        /// </summary>
        /// <returns></returns>
        public ActionResult StaffAdd(string Name, string Address, string Email, string Tel, string Remark,string index, string NativePlace, string RoleId, DateTime BirthDay, int Sex)
        {
          
            if (index == "")
            {
                return Json(_staffService.StaffAdd(Name, Address, Email, Tel, Remark,NativePlace,RoleId,BirthDay,Sex), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(_staffService.StaffUpdate(Name, Address, Email, Tel, Remark,index, NativePlace, RoleId, BirthDay,Sex), JsonRequestBehavior.AllowGet);
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult StaffDelete(string id)
        {
            var row = id.Split(',');
            int a = 0;
            foreach (var item in row)
            {
                if (_staffService.StaffDelete(id))
                {
                    a += 1;
                }
            }
            if (a == row.Length)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
        }

    }
}